part of '../version.dart';

///Update
class PatchUpdate implements Jsonable, Comparable<PatchUpdate> {
  ///目的，说明此次更新的目标是什么
  String intention;

  ///修复漏洞，消除负面作用的更新
  final List<Change> patches;

  ///功能升级，从无到有的更新
  final List<Change> updates;

  ///版本号，更新后的版本，比如"2.2.1"
  final Version version;

  PatchUpdate({
    required this.intention,
    required this.patches,
    required this.updates,
    required this.version,
  });

  /// 当从Raw格式恢复时，需要额外提供major和minor（因为它通常是在MajorUpdate里面的，主版本信息由上面提供）
  factory PatchUpdate.fromRaw(Json raw, int major, int minor) => PatchUpdate(
        intention: raw["intention"],
        patches: (raw["patches"] as List).unzip((raw) => Change.fromRaw(raw)),
        updates: (raw["updates"] as List).unzip((raw) => Change.fromRaw(raw)),
        version: Version(major, minor, raw["patch"]),
      );

  @override
  Json toJson() => {
        "intention": intention,
        "patches": patches.toJson(),
        "updates": updates.toJson(),
        "patch": version.patch,
      };

  /// 排序的依据为版本号的大小
  @override
  int compareTo(PatchUpdate other) => version.compareTo(other.version);
}
